package checkers.gui.dialogs;

import checkers.common.Settings;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 * Class OptionsDialog
 * Show game and view options dialog
 * 
 * @author Cristian Tardivo
 */
public class OptionsDialog extends javax.swing.JDialog {    
    private static final ResourceBundle lang = ResourceBundle.getBundle("checkers/common/lang");
    // dialog result
    private boolean result;
    
    /**
     * Create new OptionsDialog
     * @param parent Parent Frame
     * @param modal Modal exclude
     */
    public OptionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        // Set default values
        initComponents();
        switch (Settings.getSamplesLevel()){
            case 0: anti_aliasing.setSelectedIndex(0);break;
            case 2: anti_aliasing.setSelectedIndex(1);break;
            case 4: anti_aliasing.setSelectedIndex(2);break;
            case 8: anti_aliasing.setSelectedIndex(3);break;
            case 16: anti_aliasing.setSelectedIndex(4);break;
            default: System.err.println("Invalid Sample level in options view");break;
        }
        switch (Settings.getShadowsMapSize()){
            case 512: shadow_size.setSelectedIndex(0);break;
            case 1024: shadow_size.setSelectedIndex(1);break;
            case 2048: shadow_size.setSelectedIndex(2);break;
            default: System.err.println("Invalid Shadows Map Size in options view");break;
        }
        auto_rotation.setSelected(Settings.getAutoRotation());
        fpsLimit.setSelected(Settings.getLimitFPS());
        shadow.setSelected(Settings.getShadowsEnable());
        shadow_filter.setSelectedIndex(Settings.getShadowFilterLevel());
        show_fps.setSelected(Settings.getShowFPS());
        sound.setSelected(Settings.getAudioEnable());
        v_sync.setSelected(Settings.getVSync());
        board3d.setSelected(Settings.is3DView());
        this.getRootPane().setDefaultButton(boton_aceptar);
        // Key ESCAPE action
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){public void actionPerformed(ActionEvent e){boton_canelar.doClick();}}; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }

    /**
     * Get Action result
     * @return 
     */
    public boolean getResult(){
        return result;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shadow = new javax.swing.JCheckBox();
        sound = new javax.swing.JCheckBox();
        v_sync = new javax.swing.JCheckBox();
        auto_rotation = new javax.swing.JCheckBox();
        anti_aliasing_lbl = new javax.swing.JLabel();
        anti_aliasing = new javax.swing.JComboBox();
        shadow_filt_lbl = new javax.swing.JLabel();
        shadow_filter = new javax.swing.JComboBox();
        boton_aceptar = new javax.swing.JButton();
        boton_canelar = new javax.swing.JButton();
        show_fps = new javax.swing.JCheckBox();
        fpsLimit = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        board3d = new javax.swing.JCheckBox();
        shadow_size_lbl = new javax.swing.JLabel();
        shadow_size = new javax.swing.JComboBox();

        setTitle(lang.getString("GAME_SETTINGS")); // NOI18N
        setMinimumSize(new java.awt.Dimension(335, 190));
        setName(""); // NOI18N
        setResizable(false);

        shadow.setText(lang.getString("SHADOWS")); // NOI18N

        sound.setText(lang.getString("SOUND")); // NOI18N

        v_sync.setText(lang.getString("V-SYNC")); // NOI18N

        auto_rotation.setText(lang.getString("AUTO-ROTATION")); // NOI18N

        anti_aliasing_lbl.setText(lang.getString("ANTI-ALIASING")); // NOI18N

        anti_aliasing.setModel(new javax.swing.DefaultComboBoxModel(new String[] { lang.getString("DISABLED"), "2x", "4x", "8x", "16x" }));

        shadow_filt_lbl.setText(lang.getString("SHADOW_FILTER")); // NOI18N

        shadow_filter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { lang.getString("BILINEAR"), lang.getString("DITHER"), lang.getString("NEAREST"), lang.getString("PCF4"), lang.getString("PCF8"), lang.getString("PCFPOISSON") }));

        boton_aceptar.setText(lang.getString("ACEPT")); // NOI18N
        boton_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_aceptarActionPerformed(evt);
            }
        });

        boton_canelar.setText(lang.getString("CANCEL")); // NOI18N
        boton_canelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_canelarActionPerformed(evt);
            }
        });

        show_fps.setText(lang.getString("SHOW_FPS")); // NOI18N

        fpsLimit.setText(lang.getString("30_FPS_LIMIT")); // NOI18N
        fpsLimit.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel3.setText(lang.getString("NEED_RESET_APPLICATION")); // NOI18N

        board3d.setText(lang.getString("3D_BOARD")); // NOI18N

        shadow_size_lbl.setText(lang.getString("SHADOW_SIZE")); // NOI18N

        shadow_size.setModel(new javax.swing.DefaultComboBoxModel(new Integer[] { 512, 1024, 2048}));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fpsLimit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(boton_aceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(boton_canelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(shadow)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(sound)
                                    .addComponent(show_fps)
                                    .addComponent(auto_rotation)
                                    .addComponent(v_sync))
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(shadow_size_lbl)
                                    .addComponent(shadow_filt_lbl)
                                    .addComponent(anti_aliasing_lbl))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(anti_aliasing, 0, 104, Short.MAX_VALUE)
                                    .addComponent(shadow_filter, 0, 104, Short.MAX_VALUE)
                                    .addComponent(shadow_size, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(board3d)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(10, 10, 10)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sound)
                    .addComponent(anti_aliasing_lbl)
                    .addComponent(anti_aliasing, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(show_fps)
                    .addComponent(shadow_filt_lbl)
                    .addComponent(shadow_filter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(auto_rotation)
                    .addComponent(shadow_size_lbl)
                    .addComponent(shadow_size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0)
                .addComponent(shadow)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(v_sync)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(board3d, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fpsLimit)
                    .addComponent(boton_aceptar)
                    .addComponent(boton_canelar))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boton_canelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_canelarActionPerformed
        result = false;
        dispose();
    }//GEN-LAST:event_boton_canelarActionPerformed

    /**
     * Save Settings
     * @param evt 
     */
    private void boton_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_aceptarActionPerformed
        switch (anti_aliasing.getSelectedIndex()){
            case 0: Settings.setSamplesLevel(0);break;
            case 1: Settings.setSamplesLevel(2);break;
            case 2: Settings.setSamplesLevel(4);break;
            case 3: Settings.setSamplesLevel(8);break;
            case 4: Settings.setSamplesLevel(16);break;
            default: System.err.println("Invalid Sample level in options view");break;
        }
        switch (shadow_size.getSelectedIndex()){
            case 0: Settings.setShadowsMapSize(512);break;
            case 1: Settings.setShadowsMapSize(1024);break;
            case 2: Settings.setShadowsMapSize(2048);break;
            default: System.err.println("Invalid Shadows Map Size in options view");break;
        }
        Settings.setAutoRotation(auto_rotation.isSelected());
        Settings.setLimitFPS(fpsLimit.isSelected());
        Settings.setShadowsEnable(shadow.isSelected());
        Settings.setShadowFilterLevel(shadow_filter.getSelectedIndex());
        Settings.setShowFPS(show_fps.isSelected());
        Settings.setAudioEnable(sound.isSelected());    
        Settings.setVSync(v_sync.isSelected());
        Settings.set3DView(board3d.isSelected());
        Settings.saveSettings();
        result = true;
        dispose();
    }//GEN-LAST:event_boton_aceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox anti_aliasing;
    private javax.swing.JLabel anti_aliasing_lbl;
    private javax.swing.JCheckBox auto_rotation;
    private javax.swing.JCheckBox board3d;
    private javax.swing.JButton boton_aceptar;
    private javax.swing.JButton boton_canelar;
    private javax.swing.JCheckBox fpsLimit;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JCheckBox shadow;
    private javax.swing.JLabel shadow_filt_lbl;
    private javax.swing.JComboBox shadow_filter;
    private javax.swing.JComboBox shadow_size;
    private javax.swing.JLabel shadow_size_lbl;
    private javax.swing.JCheckBox show_fps;
    private javax.swing.JCheckBox sound;
    private javax.swing.JCheckBox v_sync;
    // End of variables declaration//GEN-END:variables
}
